require File.dirname(__FILE__)+"/test_helper"
require "starframe"

class MainScene
  attr_reader :game, :current_game
  attr_reader :screen, :screen_width, :screen_height
  attr_reader :started
  def initialize game
    @@last_scene = self
    @game = game
    @current_game = StarRuby::Game.current
    @screen = @game.screen
    @screen_width  = @screen.width
    @screen_height = @screen.height
  end
  def self.last_scene
    @@last_scene
  end
  def start
    @started = true
    nil
  end
end

class TestStarFrame < Test::Unit::TestCase
  class AlternateMainScene < MainScene
  end
  
  def test_start
    StarFrame.start
    scene = MainScene.last_scene
    assert_instance_of MainScene, scene
    assert_equal MainScene.last_scene.current_game, scene.game
    assert_equal 30, scene.game.fps
    assert_equal 640, scene.screen_width
    assert_equal 480, scene.screen_height
    assert scene.started
  end
  def test_start_with_options
    StarFrame.start :width => 320, :height => 240, :fps => 60
    scene = MainScene.last_scene
    assert_instance_of MainScene, scene
    assert_equal 60, scene.game.fps
    assert_equal 320, scene.screen_width
    assert_equal 240, scene.screen_height
  end
  def test_start_alternate_scene
    StarFrame.start :scene => AlternateMainScene
    scene = MainScene.last_scene
    assert_instance_of AlternateMainScene, scene
    assert_equal 30, scene.game.fps
    assert_equal 640, scene.screen_width
    assert_equal 480, scene.screen_height
  end
  
  def test_camelize
    assert_equal "UnderLine",               "under_line".camelize
    assert_equal "NestedModule::UnderLine", "nested_module/under_line".camelize
    assert_equal "DashLine",                "dash-line".camelize
    assert_equal "SpacedWord",              "spaced word".camelize
  end
  
  def test_integer_linear
    actuals = 10.linear
    assert_kind_of Array, actuals
    assert_equal   10,    actuals.size
    actuals.each do |actual|
      assert_kind_of Float, actual
    end
    
    expects = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
    expects.zip(actuals) do |expected_float, actual_float|
      assert_in_delta expected_float, actual_float, 0.1
    end
  end
  
  def test_integer_accelerate
    actuals = 10.accelerate
    assert_kind_of Array, actuals
    assert_equal   10,    actuals.size
    actuals.each do |actual|
      assert_kind_of Float, actual
    end
    
    expects = [0.0, 0.1, 0.4, 0.9, 1.6, 2.5, 3.6, 4.9, 6.4, 8.1]
    expects.zip(actuals) do |expected_float, actual_float|
      assert_in_delta expected_float, actual_float, 0.1
    end
  end
  
  def test_integer_decelerate
    values = 10.decelerate
    assert_kind_of Array, values
    assert_equal   10,    values.size
    [0.0, 1.9, 3.6, 5.1, 6.4, 7.5, 8.4, 9.1, 9.6, 9.9].zip(values) do |expected_float, actual_float|
      assert_in_delta expected_float, actual_float, 0.1
    end
  end
  
  def test_symbol_to_proc
    assert_equal ["0", "1", "2"], [0, 1, 2].map(&:to_s)
    assert_equal 6, [1, 2, 3].inject(0, &:+)
  end
end
